package org.niu.node;

import org.niu.tree.TreeNode;

import java.util.*;

public class Flatten {
    HashMap<Integer,Node> list = new HashMap<Integer, Node>();
    int i = 0;
    Node ans = new Node();
    Node tail = ans;
    public Node flatten(Node head) {
        flatten1(head);
        int size = list.size();
        for (int i = 0; i < list.size(); i++) {

            if(i+1 < size) list.get(i).next = list.get(i+1);
            if(i > 0) list.get(i).prev = list.get(i-1);
            list.get(i).child = null;
        }
        return head;
    }
    public void flatten1(Node head) {
        if(head == null) return;
        list.put(i++,head);
        flatten(head.child);

        flatten(head.next);
    }
}
